import Mock from 'mockjs'
import state from '../store/index.js'
function param2Obj(url) {
  const search = url.split('?')[1]
  if (!search) {
    return {}
  }
  return JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}')
}

// 股票交易
/* const stockInfo = () => {
  let stockList = [];
  for (let i = 0; i < 1; i++) {
    const stockSingle = {
      stock_id: 13434,
      stock_name: '东方财富',
      price: 12.8,
      stockMinPrice: 11.5,
      // eslint-disable-next-line no-dupe-keys
      stockMinPrice: 13.5,
      balance: 12122, // 余额
      number: 123,
    };
    stockList.push(stockSingle);
  }
  return stockList;
};
Mock.mock('myfollowStock', stockInfo); */

// followedStock
let followStock=[{
    "id": '1',
    "code": "0600795",
    "name": "国电电力",
    "followed": true,
    "count": 0,
    "stockType": "HS",
    "symbol": "600795",
    "lastestPrice":1.81,
    "percent":"-1.63%",
  },
  {
    "id": '1',
    "code": "0601899",
    "name": "紫金矿业",
    "followed": true,
    "count": 0,
    "stockType": "HS",
    "symbol": "601899",
    "lastestPrice":1.81,
    "percent":"-1.63%",
  }
]
let hotStock=[{
    "id": '1',
    "code": "0600795",
    "name": "国电电力",
    "followed": true,
    "count": 0,
    "stockType": "HS",
    "symbol": "600795",
    "lastestPrice":1.81,
    "percent":"-1.63%",
  },
  {
    "id": '2',
    "code": "0601899",
    "name": "紫金矿业",
    "followed": true,
    "count": 0,
    "stockType": "HS",
    "symbol": "601899",
    "lastestPrice":2.67,
    "percent":"0.55%",
  },
  {
    "id": '3',
    "code": "0600050",
    "name": "中国电信",
    "followed": false,
    "count": 0,
    "stockType": "HS",
    "symbol": "600050",
    "lastestPrice":4.96,
    "percent":"-0.40%",
  }
]
export default {
  getFollowStock: config => {
    console.log("--------------------------------------------getFollowStock------------------------------------------------------");
    console.log(followStock);
    return followStock
  },
  getHotStock: config => {
    return hotStock
  },
  updateFollowStock: config => {
    console.log("--------------------------------------------updateFollowStock------------------------------------------------------");
    console.log(config);
    const { code,name,symbol,followed } = param2Obj(config.url)
    // 表单提交过来后所有的表单项都已经变成了字符串，但是性格和状态在定义的时候定义的0/1，所以需要用parseInt()转换成整形
    // 如果没有转换，表单选中的值并没有真正加到表单，渲染到table中时它就会按照转换函数formatSex第三种情况未定义的值来渲染   
    console.log(followStock.some(item => item.code == code));
    console.log(code);
    console.log(name);
    console.log(followed);
    if (followed === "true") {
      console.log("unfollowed");
      followStock = followStock.filter(u => u.code !== code)
      hotStock.some(u => {
        if (u.code === code) {
          u.followed = false
          return true
        }
      })
      return {
        followStock,
        hotStock,
        code: 0,
        data: {
          message: '已取消关注'
        }
      }
    } else if (followed === "false") {
      console.log("followed");
      followStock.unshift({
        id:3,
        code: code,
        name: name,
        symbol: symbol,
        followed: true,
        count: 0,
        stockType: "HS",
        lastestPrice: 4.96,
        percent:"-0.40%"
      })
      hotStock.some(u => {
        if (u.code === code) {
          u.followed = true
          return true
        }
      })
      return {
        followStock,
        hotStock,
        code: 0,
        data: {
          message: '已关注'
        }
      }
    }    
  }
}